<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/12/15
 * Time: 10:18
 */

class Tools
{
     private  static $conn;
     private  static $host='127.0.0.1';
     private static $user='root';
     private static $password='123456';
     private static $database='moneyinfo';

     public static function init($host,$user,$password,$database){
        if(!empty($host)){
            self::$host =$host;
        }
        if(!empty($user)){
            self::$user =$user;
        }
        if (!empty($password)){
            self::$password =$password;
        }
        if(!empty($database)){
            self::$database;
        }
        //创建数据库对象
        self::$conn = new mysqli();
         //连接数据库对
         self::$conn->connect(self::$host,self::$user,self::$password,self::$database);
         //判断是否连接成功
         if(!empty(self::$conn->connect_error)){
                echo self::$conn->connect_error;
                die;
         }
     }
     //创建数据库查询语句
     public static function cx($sql){
         $data = [];
         $resulf =self::$conn->query($sql);
         if(empty(self::$conn->error)){//error是错误的意思 empty(error)就是没有错误的意思
             while (true){
                 $row = $resulf->fetch_assoc();//判断查询语句是否有值
                 if(empty($row)){
                     break;
                 }
                 $data[]=$row;//把有值的存进数组
             }
         }
     }
     public static function  noselect($sql){
         $resulf = self::$conn->query($sql);
         if ($resulf){//成功
                if(self::$conn->affected_rows>0){
                    return[
                        'status'=>true,
                        'message'=>''
                    ];
                }else{
                    return[
                        'status'=>false,
                        'message'=>'影响数为0'
                        ];

                }
         }else{

             return[
                 'status'=>false,
                 'message'=>self::$conn->error
             ];

         }
     }
     public  static function close(){
         self::$conn->close();
         //关闭数据库连接
     }

}